from typing import List

from afast_core.core_tools import json_helper
from afast_core.core_tools.snow_flake_helper import Snow
from infirmary_admin_src.infirmary_common.infirmary_controller import IBaseController
from infirmary_admin_src.infirmary_common.infirmary_controller.rest_controller_with_depends import \
    RestControllerWithDepends
from fastapi import Request, Depends, Path, Body

from infirmary_admin_src.infirmary_smart_admin.infirmary_biz.infirmary.businesses.schemas import SchemaBase
from infirmary_admin_src.infirmary_smart_admin.infirmary_biz.infirmary.businesses.services.service_businesses import \
    Businesses



class XParam(SchemaBase):
    '''
    账号密码登录参数
    '''
    pass
    appid:str
    si_number:str
    dc_number:str
    workcycle:List[str]
    worktab:str
    interval_am_sart:str
    interval_am_end:str
    interval_days: int
    interval_pm_sart: str
    interval_pm_end: str
    interval_scheduling_stock: int
    interval_time:int
    rulesname:str

class IController(IBaseController):
    '''
    定制相关约束检测函数逻辑步序
    '''

    def __init__(self, *, request: Request,
                 schema_param: XParam,
                 ):
        super().__init__(request=request)
        # 入参参数
        # 入参参数
        self.schema_param = schema_param


@RestControllerWithDepends()
class DoctorStationRuleInfoAddNewUserController(IController):

    def business_login(self):
        pass
        order = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
        self.schema_param.workcycle = sorted(list(set(self.schema_param.workcycle)), key=lambda x: order.index(x))
        rulesid = Snow('RULESID', 5).get(is_random=False)
        updata = {
            'status': 0,
            'rulesid':  rulesid,
            'rulesname':self.schema_param.rulesname,
            'mode':1,
            'mode_workcycle_rules':json_helper.dict_to_json(self.schema_param.dict(include={'interval_am_sart',
                                                                                            'interval_am_end',
                                                                                            'interval_days',
                                                                                            'interval_pm_sart',
                                                                                            'interval_pm_end',
                                                                                            'interval_scheduling_stock',
                                                                                            'interval_time',
                                                                                            'workcycle'
                                                                                            }))
        }
        #  rulesid = TextField(constraints=[SQL("DEFAULT ''::text")], index=True, verbose_name="规则ID")
        #  rulesname= TextField(constraints=[SQL("DEFAULT ''::text")], verbose_name="规则名称")
        #  mode = IntegerField(constraints=[SQL("DEFAULT 0")], null=True, verbose_name="0： 按间隔时间生成排班 1：自定义时段序")
        #  mode_workcycle_rules = TextField(constraints=[SQL("DEFAULT ''::text")], verbose_name="坐诊周期排班规则")
        #  status = IntegerField(constraints=[SQL("DEFAULT 0")], null=True, verbose_name="是否停用 0 否 1 是")
        #  ctime = DateTimeField(constraints=[SQL("DEFAULT now()")], null=True, verbose_name="创建时间")
        #  utime = DateTimeField(constraints=[SQL("DEFAULT now()")], null=True, verbose_name="更新时间")
        Businesses.addnewuser_doctor_station_rule_info(**updata)
        Businesses.update_doctor_station_rule_info_usernewrulesid(
            rulesid= rulesid, appid=self.schema_param.appid,
            si_number=self.schema_param.si_number,
            dc_number=self.schema_param.dc_number,
        )
        # Businesses.update_doctor_station_rule_info(rulesid=self.schema_param.rulesid,**updata)
        # sorted_data = sorted(data)
        self.response_msg='新增规则并应用成功！'
        return



